#!/usr/bin/python2.4
#
# Copyright 2010 Google Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest
import bitarray

class BitarrayTest(unittest.TestCase):
  def testWordCross(self):
    a = bitarray.BitArray(5)
    b = bitarray.BitArray(5)
    a[2] = True
    a.Cross(0,b)
    self.failUnless(b[2])
    self.failUnless(not a[2])

  def testBitCross(self):
    a = bitarray.BitArray(5)
    b = bitarray.BitArray(5)
    a[2] = True
    a.Cross(1,b)
    self.failUnless(b[2])
    self.failUnless(not a[2])

  def testBitAndWordCross(self):
    a = bitarray.BitArray(55)
    b = bitarray.BitArray(55)
    a[2] = True
    a[43] = True
    a.Cross(1,b)
    self.failUnless(b[2])
    self.failUnless(not a[2])
    self.failUnless(b[43])
    self.failUnless(not a[43])

  def testBitAndWordCrossBack(self):
    a = bitarray.BitArray(55)
    b = bitarray.BitArray(55)
    a[2] = True
    a[51] = True
    b[53] = True
    a.Cross(50, b)
    self.failUnless(a[2])
    self.failUnless(not b[2])
    self.failUnless(b[51])
    self.failUnless(not a[51])

  def testAllBitCross(self):
    b = bitarray.BitArray(200)
    for i in xrange(0, 200):
      b[i] = True
    for i in range(0, 200):
      a = bitarray.BitArray(200)
      bp = bitarray.BitArray(200)
      bp.Copy(b)
      a.Cross(i, bp)
      if i < 199:
        self.failUnless(a[i + 1])
      if i > 0:
        self.failUnless(not a[i - 1])
      self.failUnless(a[i])


if __name__ == '__main__':
  unittest.main()
